package com.example.layui.demo.controller;


import com.example.layui.demo.entity.*;
import com.example.layui.demo.service.StuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

//@RestController
@Controller
@CrossOrigin//跨域注解方式
public class Mycontrolller {


    @PostMapping("/login")
    @ResponseBody
    public LoginUser login(LoginUser user)
    {
        System.out.println(user.getUsername());

        System.out.println(user.getPassword());

       return user;

    }

    @GetMapping("/test")

    public String gotoTestPage() {
        return "test";
    }

    @GetMapping("/")

    public String gotoRegisterPage() {
        return "echarsDemo";
    }

    @PostMapping("/register")
    @ResponseBody
    public Map<String, String> regsiter(Customer customer) {
        String key = "msg";
        String msg = "注册成功了";
        Map<String, String> map = new HashMap<>();
        map.put(key, msg);
        return map;

    }

    @Autowired
    StuService stuService;

    @GetMapping("/getResult")
    @ResponseBody
    public ResultEntity getResult() {
        List<StuCategaryVo> result = stuService.getResult();
        ResultEntity resultEntity = new ResultEntity(0, "查询", result);
        return resultEntity;

    }


    @GetMapping("/list")

    public String gotoListPage() {
        return "list";
    }




    @PostMapping("/getArticlePageList")
    @ResponseBody
    public ResultEntity<List<Article>> getArticlePageList(ArticleVo vo) {
        if (null == vo.getPage()) {
            vo.setPage(1);
        }
        int start = (vo.getPage() - 1) * vo.getLimit();//每次请求的开始的记录的下标
        vo.setStart(start);
        Integer articleCount = stuService.getArticleCount(vo);//拿到总记录数 用于计算总页数
        List<Article> list = stuService.getArticlePageList(vo);
        ResultEntity resultEntity = new ResultEntity(0, "查询",articleCount, list);
        return resultEntity;


    }

    @GetMapping("/getArticleById/{id}")
    @ResponseBody
    public ResultEntity<Article> getArticleById(@PathVariable("id") Integer id) {

        Article article = stuService.getArticle(id);
        ResultEntity resultEntity = new ResultEntity(0, "查询", article);
        return resultEntity;

    }


    @GetMapping("/delArticleById/{id}")
    @ResponseBody
    public ResultEntity delArticleById(@PathVariable("id") Integer id) {
        Integer del = stuService.delArticlebyId(id);
        ResultEntity resultEntity = new ResultEntity(0, "查询", del);
        return resultEntity;

    }


    @PostMapping("/updArticleById")
    @ResponseBody
    public ResultEntity updArticleById(Article article) {


        Integer update = stuService.updArticleById(article);
        ResultEntity resultEntity = new ResultEntity(0, "查询", update);
        return resultEntity;

    }


    @GetMapping("/delArticleByIds")
    @ResponseBody
    public ResultEntity delArticleByIds(Integer[] ids) {
        if (ids == null) {
            return null;
        }
        ResultEntity resultEntity = new ResultEntity(0, "查询", stuService.delArticlebyIds(ids));
        return resultEntity;

    }







}
